package de.jmda.core.util.fileset;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import de.jmda.core.util.fileset.DirectoryFileFilter;
import de.jmda.core.util.fileset.FileFilterDirectoriesAndJarsOnly;
import de.jmda.core.util.fileset.FileFilterDirectoriesOnly;
import de.jmda.core.util.fileset.FileFilterJavaSourceFilesOnly;

public class JUTDirectoryFileFilter
{
	private final static File FILE = new File("tmp/jaxb.test.xml");

	private static JAXBContext jaxbContext;

	private Marshaller marshaller;
	
	private Unmarshaller unmarshaller;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		FileUtils.touch(FILE);

		jaxbContext =
				JAXBContext.newInstance(
						new Class[]
						    {
									DirectoryFileFilter.class,
									FileFilterDirectoriesOnly.class,
									FileFilterDirectoriesAndJarsOnly.class,
									FileFilterJavaSourceFilesOnly.class,
								});
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
		FileUtils.deleteQuietly(FILE);
		jaxbContext = null;
	}

	@Before
	public void setUp() throws Exception
	{
		marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		
		unmarshaller = jaxbContext.createUnmarshaller();
	}

	@After
	public void tearDown() throws Exception
	{
		marshaller = null;
		unmarshaller = null;
	}
	
	@Test
	public void test() throws JAXBException, IOException
	{
		DirectoryFileFilter directoryFileFilterDirectoriesOnly =
				new DirectoryFileFilter(
						new File(FILE.getParentFile().getPath()),
						new FileFilterDirectoriesOnly());
	//		DirectoryFileFilter directoryFileFilterDirectoriesOrJarsOnly =
	//			new DirectoryFileFilter(
	//					new File("c:/temp"),
	//					new FileFilterDirectoriesAndJarsOnly());
	//		DirectoryFileFilter directoryFileFilterJavaFilesOnly =
	//			new DirectoryFileFilter(
	//					new File("c:/temp"),
	//					new FileFilterJavaSourceFilesOnly());
		
		FileOutputStream fileOutputStream = new FileOutputStream(FILE);
		
		marshaller.marshal(directoryFileFilterDirectoriesOnly, fileOutputStream);
	//		marshaller.marshal(directoryFileFilterDirectoriesOrJarsOnly, System.out);
	//		marshaller.marshal(directoryFileFilterJavaFilesOnly, System.out);
		
		fileOutputStream.flush();
		fileOutputStream.close();
		
		DirectoryFileFilter directoryFileFilter =
				(DirectoryFileFilter) unmarshaller.unmarshal(FILE);
		System.out.println(
				"file filter type " + directoryFileFilter.getFileFilter().getClass());
	}
}